

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class MemoryConsumption {
	
	public final static int SIZE = 10000;

	public static void main(String[] args) {
		
		Runtime runtime = Runtime.getRuntime();
		long memoryBefore;
		long memoryAfter;
		double memoryConsumption;
		int i;
		
		System.out.println("Memory consumption test\n");
		//Array
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		
		int[] array = new int[SIZE];
		for(i = 0 ; i < SIZE; i++) {
			array[i] = i;
		}
		
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("Array:\t\t" + memoryConsumption);

		
		//ArrayList
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		
		List<Integer> arrayList = new ArrayList<Integer>();
		for(i = 0 ; i < SIZE; i++) {
			arrayList.add(i);	
		}
		
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("ArrayList:\t" + memoryConsumption );
		
		
		//LinkedList
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		
		List<Integer> linkedList = new LinkedList<Integer>();
		for(i = 0 ; i < SIZE; i++) {
			linkedList.add(i);	
		}
		
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("LinkedList:\t" + memoryConsumption);
		
		
		//TreeMap
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		Map<Integer, Integer> tree = new TreeMap<Integer, Integer>();

		for(i = 0 ; i < SIZE; i++) {
			tree.put(i, i);
		}
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("TreeMap:\t" + memoryConsumption);
		
		
		//HashMap
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		for(i = 0 ; i < SIZE; i++) {
			hashMap.put(i, i);	
		}
		
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("HashMap:\t" + memoryConsumption);
		
		
		//IntHashMap
		System.gc();
		memoryBefore = runtime.totalMemory() - runtime.freeMemory();
		IntHashMap intHashMap = new IntHashMap();

		for(i = 0 ; i < SIZE; i++) {
			intHashMap.put((int)Math.pow(i, 2), i);	
		}
		
		System.gc();
		memoryAfter = runtime.totalMemory() - runtime.freeMemory();
		memoryConsumption = (double) (memoryAfter-memoryBefore) / SIZE;
		System.out.println("IntHashMap: \t" + memoryConsumption);
		
	}
}
